!*******************************************************
!Program to output data for value and policy functions
!*******************************************************
SUBROUTINE output_valuefunction(value6,task,V_0,V_11, V_21, V_31, V_12, V_22, V_32, V_13, V_23, V_33, &
     V_14, V_24, V_34, V_15, V_25, V_35, V_16, V_26, V_36, V_17, V_27, V_37, V_18, V_28, V_38, phi_1_star,phi_2_star,phi_3_star)




USE mod_types
!USE mod_grid
USE mod_values


IMPLICIT NONE


INTEGER:: io1
REAL(dp), DIMENSION (phi_grid_size,1426), INTENT(IN):: V_11, V_21, V_31, V_12, V_22, V_32, V_13, V_23, V_33, &
     V_14, V_24, V_34, V_15, V_25, V_35, V_16, V_26, V_36, V_17, V_27, V_37, V_18, V_28, V_38  
REAL(dp), DIMENSION (phi_grid_size), INTENT(IN)::	 value6, V_0           
INTEGER, DIMENSION (phi_grid_size), INTENT(IN):: task
REAL(dp), INTENT(IN):: phi_1_star, phi_2_star, phi_3_star



OPEN(unit=12,  file="value6.txt", status="replace")
OPEN(unit=121, file="V_0.txt", status="replace")

OPEN(unit=13,  file="V_16.txt", status="replace")
OPEN(unit=14,  file="V_26.txt", status="replace")
OPEN(unit=15,  file="V_36.txt", status="replace")

OPEN(unit=53,  file="V_17.txt", status="replace")
OPEN(unit=54,  file="V_27.txt", status="replace")
OPEN(unit=55,  file="V_37.txt", status="replace")


OPEN(unit=56,  file="V_18.txt", status="replace")
OPEN(unit=57,  file="V_28.txt", status="replace")
OPEN(unit=58,  file="V_38.txt", status="replace")


!OPEN(unit=16,  file="value1.txt", status="replace")
!!OPEN(unit=17,  file="V_0.txt", status="replace")
OPEN(unit=18,  file="V_11.txt", status="replace")
OPEN(unit=19,  file="V_21.txt", status="replace")
OPEN(unit=20,  file="V_31.txt", status="replace")

!OPEN(unit=21,  file="value2.txt", status="replace")
!!OPEN(unit=22,  file="V_0.txt", status="replace")
OPEN(unit=23,  file="V_12.txt", status="replace")
OPEN(unit=24,  file="V_22.txt", status="replace")
OPEN(unit=25,  file="V_32.txt", status="replace")

!OPEN(unit=26,  file="value3.txt", status="replace")
!!OPEN(unit=27,  file="V_0.txt", status="replace")
OPEN(unit=28,  file="V_13.txt", status="replace")
OPEN(unit=29,  file="V_23.txt", status="replace")
OPEN(unit=30,  file="V_33.txt", status="replace")


!OPEN(unit=31,  file="value4.txt", status="replace")
!!OPEN(unit=32,  file="V_0.txt", status="replace")
OPEN(unit=33,  file="V_14.txt", status="replace")
OPEN(unit=34,  file="V_24.txt", status="replace")
OPEN(unit=35,  file="V_34.txt", status="replace")


!OPEN(unit=36,  file="value5.txt", status="replace")
!!OPEN(unit=37,  file="V_0.txt", status="replace")
OPEN(unit=38,  file="V_15.txt", status="replace")
OPEN(unit=39,  file="V_25.txt", status="replace")
OPEN(unit=40,  file="V_35.txt", status="replace")



WRITE(12,*) 'value6'
WRITE(12,*) " "
WRITE(121,*) 'V_0'
WRITE(121,*) " "
WRITE(13,*) 'V_16'
WRITE(13,*) " "
WRITE(14,*) 'V_26'
WRITE(14,*) " "
WRITE(15,*) 'V_36'
WRITE(15,*) " "

!WRITE(16,*) 'value1'
!WRITE(16,*) " "
!WRITE(17,*) 'V_0'
!WRITE(17,*) " "
WRITE(18,*) 'V_11'
WRITE(18,*) " "
WRITE(19,*) 'V_21'
WRITE(19,*) " "
WRITE(20,*) 'V_31'
WRITE(20,*) ""



!WRITE(21,*) 'value2'
!WRITE(21,*) " "
!WRITE(22,*) 'V_0'
!WRITE(22,*) " "
WRITE(23,*) 'V_12'
WRITE(23,*) " "
WRITE(24,*) 'V_22'
WRITE(24,*) " "
WRITE(25,*) 'V_32'
WRITE(25,*) ""


!WRITE(26,*) 'value3'
!WRITE(26,*) " "
!WRITE(27,*) 'V_0'
!WRITE(27,*) " "
WRITE(28,*) 'V_13'
WRITE(28,*) " "
WRITE(29,*) 'V_23'
WRITE(29,*) " "
WRITE(30,*) 'V_33'
WRITE(30,*) ""


!WRITE(31,*) 'value4'
!WRITE(31,*) " "
!WRITE(32,*) 'V_0'
!WRITE(32,*) " "
WRITE(33,*) 'V_14'
WRITE(33,*) " "
WRITE(34,*) 'V_24'
WRITE(34,*) " "
WRITE(35,*) 'V_34'
WRITE(35,*) ""



!WRITE(36,*) 'value5'
!WRITE(36,*) " "
!WRITE(37,*) 'V_0'
!WRITE(37,*) " "
WRITE(38,*) 'V_15'
WRITE(38,*) " "
WRITE(39,*) 'V_25'
WRITE(39,*) " "
WRITE(40,*) 'V_35'
WRITE(40,*) ""


WRITE(53,*) 'V_17'
WRITE(53,*) " "
WRITE(54,*) 'V_27'
WRITE(54,*) " "
WRITE(55,*) 'V_37'
WRITE(55,*) ""


WRITE(56,*) 'V_18'
WRITE(56,*) " "
WRITE(57,*) 'V_28'
WRITE(57,*) " "
WRITE(58,*) 'V_38'
WRITE(58,*) ""


io1 = 0
DO io1=1,phi_grid_size

  
  WRITE(12,*)  value6(io1)
  WRITE(121,*) V_0(io1)
  WRITE(13,*)  V_16(io1,1)
  WRITE(14,*)  V_26(io1,1)
  WRITE(15,*)  V_36(io1,1)

  !WRITE(16,*) value1(io1)
  !WRITE(17,*)  V_0(io1)
  WRITE(18,*)  V_11(io1,1)
  WRITE(19,*)  V_21(io1,1)
  WRITE(20,*)  V_31(io1,1)

  !WRITE(21,*) value2(io1)
  !WRITE(22,*)  V_0(io1)
  WRITE(23,*)  V_12(io1,1)
  WRITE(24,*)  V_22(io1,1)
  WRITE(25,*)  V_32(io1,1)


  !WRITE(26,*) value3(io1)
  !WRITE(27,*)  V_0(io1)
  WRITE(28,*)  V_13(io1,1)
  WRITE(29,*)  V_23(io1,1)
  WRITE(30,*)  V_33(io1,1)

  
  !WRITE(32,*) V_0(io1)
  WRITE(33,*) V_14(io1,1)
  WRITE(34,*) V_24(io1,1)
  WRITE(35,*) V_34(io1,1)
  

  !WRITE(37,*)  V_0(io1)
  WRITE(38,*)  V_15(io1,1)
  WRITE(39,*)  V_25(io1,1)
  WRITE(40,*)  V_35(io1,1)

  WRITE(53,*) V_17(io1,1)
  WRITE(54,*) V_27(io1,1)
  WRITE(55,*) V_37(io1,1)
  


  WRITE(56,*) V_18(io1,1)
  WRITE(57,*) V_28(io1,1)
  WRITE(58,*) V_38(io1,1)
  

END DO


CLOSE(unit=12)
CLOSE(unit=121)
CLOSE(unit=13)
CLOSE(unit=14)
CLOSE(unit=15)

!CLOSE(unit=16)
!CLOSE(unit=17)
CLOSE(unit=18)
CLOSE(unit=19)
CLOSE(unit=20)

!CLOSE(unit=21)
!CLOSE(unit=22)
CLOSE(unit=23)
CLOSE(unit=24)
CLOSE(unit=25)

!CLOSE(unit=26)
!CLOSE(unit=27)
CLOSE(unit=28)
CLOSE(unit=29)
CLOSE(unit=30)

!CLOSE(unit=32)
CLOSE(unit=33)
CLOSE(unit=34)
CLOSE(unit=35)


!CLOSE(unit=37)
CLOSE(unit=38)
CLOSE(unit=39)
CLOSE(unit=40)

CLOSE(unit=53)
CLOSE(unit=54)
CLOSE(unit=55)

CLOSE(unit=56)
CLOSE(unit=57)
CLOSE(unit=58)

END SUBROUTINE output_valuefunction



